﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MemoLib.Tasks;
using MemoLib;
using MemoLib.Entity;

namespace UI
{
    public partial class frmCycleCreateUpdateTask : BaseForm
    {
        private OperationMode OpMode;
        public frmCycleCreateUpdateTask(OperationMode pMode)
        {
            InitializeComponent();
            this.OpMode = pMode;
        }

        private CycleTaskCard taskCard = null;

        public frmCycleCreateUpdateTask(CycleTaskCard pCard, OperationMode pMode)
        {
            InitializeComponent();
            this.taskCard = pCard;

            this.OpMode = pMode;
        }

        /// <summary>
        /// 确认按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
            {
                MessageBox.Show(sbForErrorMsg.ToString());
                sbForErrorMsg.Clear();
                return;
            }

            CycleTaskCard card = null;
            if (this.OpMode == OperationMode.Create || this.OpMode == OperationMode.Copy)
            {
                card = new CycleTaskCard();
                card.CardID = Int64.Parse(Utility.GenerateID());
            }
            else
            {
                card = this.taskCard;
            }
            card.ExecuteType = this.combType.SelectedItem.ToString();
            card.Frequency = (FrequencyType)Enum.Parse(typeof(FrequencyType), this.combFrequency.SelectedItem.ToString());
            card.Month = UtilityConvert.ParseInt(this.txtMonth.Text, 0);
            card.Weekday = UtilityConvert.ParseInt(this.combWeekday.SelectedItem.ToString(),0);
            card.Quantity = int.Parse(txtQuantity.Text);
            card.Day = UtilityConvert.ParseInt(this.txtDay.Text, 0);
            card.Hour = UtilityConvert.ParseInt(this.txtHour.Text, 0);
            card.Minute = UtilityConvert.ParseInt(this.txtMinute.Text, 0);
            card.Context = this.txtContext.Text;
            card.IfOnlyWorkday = this.chkOnlyWorkday.Checked;

            card.Context += Utility.GetCombineWebsiteAddresses(this.txtWebsite1.Text, this.txtWebsite2.Text);
            
            ExcelOperator op = new ExcelOperator(ConfigOperator.GetExcelPath());
            op.OpenConn();

            // 新增记录
            if (this.OpMode == OperationMode.Create || this.OpMode == OperationMode.Copy)
            {
                op.InsertCycleTask(card);
                // 写NextExecuteDate字段
                CycleTask task = new CycleTask();
                task.Card = card;
                task.WriteNextExecuteTime(op);
            }
            // 修改记录
            else
            {
                op.UpdateCycleTaskRowValue(card);
            }

            op.CloseConn();
            this.Close();
        }

        
           
        private void frmCycleCreateUpdateTask_Load(object sender, EventArgs e)
        {
            this.combFrequency.SelectedIndex = 0;
            this.combType.SelectedIndex = 0;
            this.txtQuantity.Text = "1";
            this.combWeekday.SelectedIndex = 0;
            this.combWeekday.Visible = false;

            ToSetDisplayWeekdayAndChkWorkday();

            if (this.OpMode == OperationMode.Update || this.OpMode == OperationMode.Copy)
            {
                this.combType.Text = this.taskCard.ExecuteType;
                this.combFrequency.Text = this.taskCard.Frequency.ToString();
                this.combWeekday.Text = this.taskCard.Weekday.Value.ToString();
                this.txtQuantity.Text = this.taskCard.Quantity.ToString();
                this.txtMonth.Text = this.taskCard.Month.ToString();
                this.txtDay.Text = this.taskCard.Day.ToString();
                this.txtHour.Text = this.taskCard.Hour.ToString();
                this.txtMinute.Text = this.taskCard.Minute.ToString();
                ContextAndWebsites caw = Utility.SplitContextAndWebsite(this.taskCard.Context);
                this.txtContext.Text = caw.Conterxt;
                this.txtWebsite1.Text = caw.Website;
                this.txtWebsite2.Text = caw.Website2;
                this.chkOnlyWorkday.Checked = this.taskCard.IfOnlyWorkday;
            }
        }

        private void combFrequency_SelectedIndexChanged(object sender, EventArgs e)
        {
            ToSetDisplayWeekdayAndChkWorkday();
        }

        private void ToSetDisplayWeekdayAndChkWorkday()
        {
            bool visible = this.combFrequency.SelectedItem.ToString().ToLower() == "week";
            this.combWeekday.Visible = visible;
            this.lblWeekday.Visible = visible;

            this.chkOnlyWorkday.Visible = this.combFrequency.SelectedItem.ToString().ToLower() == "day";
        }

        protected override bool CheckInput()
        {
            bool ret = false;
            switch (this.combFrequency.SelectedItem.ToString().ToLower())
            {
                case "month":
                    ret = NotNullEmpthCheck(this.txtDay.Text, "日") & NotNullEmpthCheck(this.txtHour.Text, "时间") & NotNullEmpthCheck(this.txtMinute.Text, "时间");
                    break;
                case "week":
                    ret = NotNullEmpthCheck(this.combWeekday.SelectedItem.ToString(), "星期") & NotNullEmpthCheck(this.txtHour.Text, "时间") & NotNullEmpthCheck(this.txtMinute.Text, "时间");
                    break;
                case "day":
                    ret = NotNullEmpthCheck(this.txtHour.Text, "时间") & NotNullEmpthCheck(this.txtMinute.Text, "时间");
                    break;
                case "hour":
                    ret = NotNullEmpthCheck(this.txtMinute.Text, "时间");
                    break;
            }

            ret = ret & NotNullEmpthCheck(txtContext.Text, "内容");

            if (this.OpMode == OperationMode.Copy && (
                this.combType.Text == this.taskCard.ExecuteType &&
                this.combFrequency.Text == this.taskCard.Frequency.ToString() &&
                this.combWeekday.Text == Convert.ToString(this.taskCard.Weekday.Value) &&
                this.txtQuantity.Text == Convert.ToString(this.taskCard.Quantity) &&
                this.txtMonth.Text == Convert.ToString(this.taskCard.Month) &&
                this.txtDay.Text == Convert.ToString(this.taskCard.Day) &&
                this.txtHour.Text == Convert.ToString(this.taskCard.Hour) &&
                this.txtMinute.Text == Convert.ToString(this.taskCard.Minute) &&
                this.txtContext.Text == this.taskCard.Context))
            {
                this.sbForErrorMsg.AppendLine("复制的数据必须有改动");
                ret = false;
            }
            return ret;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
